package com.nine.pesys.model.service.impl;

import com.nine.pesys.base.BaseSupportImpl;
import com.nine.pesys.model.entity.Role;
import com.nine.pesys.model.entity.User;
import com.nine.pesys.model.service.RoleService;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by L先森 on 2014/12/27.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class RoleServiceImpl extends BaseSupportImpl<Role> implements RoleService{

    @Override
    public List<Role> getByUser(User user) {
        if (user!=null) {
            list = this.getSession().createCriteria(Role.class)
                    .createAlias("user", "u")
                    .add(Restrictions.eq("u.id", user.getId()))
                    .list();
            return list;
        }
        return null;
    }

    @Override
    public List<Role> getByName(String privilegeName) {
        list = this.getSession().createCriteria(Role.class)
                .add(Restrictions.eq("name",privilegeName))
                .list();
        return list;
    }

    @Override
    public List<Role> getByCondition(Role role) {
        String sql = "from Role r where 1=1";
        if (role.getId()!=null)
            sql += " and r.id ="+role.getId();
        if (role.getName()!=null)
            sql += " and r.name = '"+role.getName()+"'";
        System.out.println(sql+"=========这是sql语句==========");
        return getSession().createQuery(sql).list();
    }
}
